﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ScarecrowLibrary.Network;

namespace Scarecrow_Network_Manager
{
    /// <summary>
    /// Logica di interazione per AdapterWindow.xaml
    /// </summary>
    public partial class AdapterWindow : Window
    {
        #region Properties
        public scAdapterProfile Profile
        { get; private set; }
        #endregion

        #region Fields
        scAdapterProfile original;
        #endregion

        #region Methods
        public AdapterWindow(scAdapterProfile adapter)
        {
            this.Profile = new scAdapterProfile();
            this.Profile.NetworkAdapter = adapter.NetworkAdapter;
            InitializeComponent();
            this.original = adapter;
            this.lblAdapter.Content = adapter.NetworkAdapter;
        }
        private void rbStaticIP_Checked(object sender, RoutedEventArgs e)
        {
            this.grpbIP.IsEnabled = true;
            this.rbStaticDNS.IsChecked = true;
            this.rbDHCPDNS.IsEnabled = false;
            this.Profile.IsIPDhcp = false;
        }

        private void rbDHCPIP_Checked(object sender, RoutedEventArgs e)
        {
            this.grpbIP.IsEnabled = false;
            if(this.rbDHCPDNS != null)
                this.rbDHCPDNS.IsEnabled = true;
            this.Profile.IPAddress = this.Profile.SubnetMask = this.Profile.Gateway = "";
            this.Profile.IsIPDhcp = true;
        }

        private void rbDHCPDNS_Checked(object sender, RoutedEventArgs e)
        {
            this.grpbDNS.IsEnabled = false;
            this.Profile.DNS1 = this.Profile.DNS2 = "";
            this.Profile.IsDNSDhcp = true;
        }

        private void rbStaticDNS_Checked(object sender, RoutedEventArgs e)
        {
            this.grpbDNS.IsEnabled = true;
            this.Profile.IsDNSDhcp = false;
        }
        private void rbDisabled_Checked(object sender, RoutedEventArgs e)
        {
            this.IPGrid.IsEnabled = this.DNSGrid.IsEnabled = false;
            if (this.Profile != null)
                this.Profile.Enabled = false;
        }

        private void rbEnabled_Checked(object sender, RoutedEventArgs e)
        {
            this.IPGrid.IsEnabled = this.DNSGrid.IsEnabled = true;
            if (this.Profile != null)
                this.Profile.Enabled = true;
        }
        bool IPCheck()
        {
            bool error = false;
            try
            {
                string s = this.txtIPAddr.Text.Trim();
                if ((bool)this.rbStaticIP.IsChecked)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        IPAddress ip = IPAddress.Parse(s);
                        this.Profile.IPAddress = ip.ToString();
                        this.lblIP.Foreground = Brushes.White;
                    }
                    else
                    {
                        this.lblIP.Foreground = Brushes.Red;
                        error = true;
                    }
                }
                else
                {
                    this.Profile.IPAddress = "";
                }
            }
            catch
            {
                this.lblIP.Foreground = Brushes.Red;
                error = true;
            }
            return error;
        }
        bool SubnetCheck()
        {
            bool error = false;
            try
            {
                string s = this.txtSubnet.Text.Trim();
                if ((bool)this.rbStaticIP.IsChecked)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        IPAddress ip = IPAddress.Parse(s);
                        this.Profile.SubnetMask = ip.ToString();
                        this.lblSubnet.Foreground = Brushes.White;
                    }
                    else
                    {
                        this.lblSubnet.Foreground = Brushes.Red;
                        error = true;
                    }
                }
                else
                {
                    this.Profile.SubnetMask = "";
                }
            }
            catch
            {
                this.lblSubnet.Foreground = Brushes.Red;
                error = true;
            }
            return error;
        }
        bool GatewayCheck()
        {
            bool error = false;
            try
            {
                string s = this.txtGateway.Text.Trim();
                if (!string.IsNullOrWhiteSpace(s))
                {
                    IPAddress ip = IPAddress.Parse(s);
                    this.Profile.Gateway = ip.ToString();
                    this.lblGateway.Foreground = Brushes.White;
                }
                else
                {
                    this.Profile.Gateway = "";
                }
            }
            catch
            {
                if (!string.IsNullOrWhiteSpace(this.Profile.Gateway))
                {
                    this.lblGateway.Foreground = Brushes.Red;
                    error = true;
                }
                else
                {
                    this.lblGateway.Foreground = Brushes.White;
                }
            }
            return error;
        }
        bool DNS1Check()
        {
            bool error = false;
            try
            {
                string s = this.txtDNS1.Text.Trim();
                if ((bool)this.rbStaticDNS.IsChecked)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        IPAddress ip = IPAddress.Parse(s);
                        this.Profile.DNS1 = ip.ToString();
                        this.lblDNS1.Foreground = Brushes.White;
                    }
                    else
                    {
                        this.Profile.DNS1 = "";
                    }
                }                
                else
                {
                    this.Profile.DNS1 = "";
                }
            }
            catch
            {
                if (!string.IsNullOrWhiteSpace(this.Profile.Gateway))
                {
                    this.lblDNS1.Foreground = Brushes.Red;
                    error = true;
                }
            }
            return error;
        }
        bool DNS2Check()
        {
            bool error = false;
            try
            {
                string s = this.txtDNS2.Text.Trim();
                if ((bool)this.rbStaticDNS.IsChecked)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        IPAddress ip = IPAddress.Parse(s);
                        this.Profile.DNS2 = ip.ToString();
                        this.lblDNS1.Foreground = Brushes.White;
                    }
                    else
                    {
                        this.Profile.DNS2 = "";
                    }
                }
                else
                {
                    this.Profile.DNS2 = "";
                }
            }
            catch
            {
                if (!string.IsNullOrWhiteSpace(this.Profile.Gateway))
                {
                    this.lblDNS2.Foreground = Brushes.Red;
                    error = true;
                }
            }
            return error;
        }
        private void txtIPAddr_TextChanged(object sender, TextChangedEventArgs e)
        {
            IPCheck();
        }

        private void txtSubnet_TextChanged(object sender, TextChangedEventArgs e)
        {
            SubnetCheck();            
        }

        private void txtGateway_TextChanged(object sender, TextChangedEventArgs e)
        {
            GatewayCheck();
        }

        private void txtDNS1_TextChanged(object sender, TextChangedEventArgs e)
        {
            DNS1Check();
        }

        private void txtDNS2_TextChanged(object sender, TextChangedEventArgs e)
        {
            DNS2Check();
        }

        private void cmdSave_Click(object sender, RoutedEventArgs e)
        {
            //Data validation
            if ( IPCheck() || SubnetCheck() || GatewayCheck() || DNS1Check() || DNS2Check())
            {
                return;
            }
            this.Close();
        }

        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Profile = null;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.rbEnabled.IsChecked = this.original.Enabled;
                this.rbDisabled.IsChecked = !this.original.Enabled;

                this.rbDHCPIP.IsChecked = this.original.IsIPDhcp;
                this.rbStaticIP.IsChecked = !this.original.IsIPDhcp;
                this.txtIPAddr.Text = this.original.IPAddress;
                this.txtSubnet.Text = this.original.SubnetMask;
                this.txtGateway.Text = this.original.Gateway;

                this.rbDHCPDNS.IsChecked = this.original.IsDNSDhcp;
                this.rbStaticDNS.IsChecked = !this.original.IsDNSDhcp;
                this.txtDNS1.Text = this.original.DNS1;
                this.txtDNS2.Text = this.original.DNS2;                
            }
            catch (Exception exc)
            {
                Global.NotifyCrash(Properties.Resources.Error_AdapterWindow, exc);
            }
        }
        #endregion
    }
}
